package cn.ym.controller;

import cn.ym.domain.User;
import cn.ym.exception.BusinessException;
import cn.ym.query.UserQuery;
import cn.ym.service.UserService;
import cn.ym.utils.AjaxResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
/**
 * 管理端登录参数的接口
 */
public class SystemController {
    @Autowired
    private UserService userService;

    //登录接口
    @PostMapping("/login")
    public AjaxResult login(@RequestBody Map<String,String> map, HttpSession session){
        try {
            User dbUser = userService.login(map);
            //将登录成功之后将登录信息放在Session中：
            session.setAttribute("user_in_session",dbUser);
            //将登陆信息响应给前端
            return  AjaxResult.me().setResultObj(dbUser);
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            //系统异常500，如果不是业务异常就是系统异常了
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，稍后重试");
        }
    }

    //退出接口
    @PostMapping("/logout")
    public AjaxResult logout( HttpSession session){
        try {

            session.removeAttribute("user_in_session");

            return  AjaxResult.me().setSuccess(true);
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            //系统异常500，如果不是业务异常就是系统异常了
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，稍后重试");
        }
    }






}
